package schoolWork.work05.test5c;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame {

    double num1 = Math.random() * 100;
    double num2 = Math.random() * 100;

    JFrame jFrame = new JFrame();

    JButton btn1 = new JButton("产生");
    JButton btn2 = new JButton("计算");

    JLabel jLabel1 = new JLabel("数字一");
    JLabel jLabel2 = new JLabel("数字二");
    JLabel jLabel3 = new JLabel("结果");

    JTextField jTextField1 = new JTextField(10);
    JTextField jTextField2 = new JTextField(10);
    JTextField jTextField3 = new JTextField(10);

    public MyJFrame() {
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();

        jPanel1.setLayout(new GridLayout(3, 2));
        jPanel1.add(jLabel1);
        jPanel1.add(jTextField1);
        jPanel1.add(jLabel2);
        jPanel1.add(jTextField2);
        jPanel1.add(jLabel3);
        jPanel1.add(jTextField3);

        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btn1) {
                    jTextField1.setText(num1 + "");
                    jTextField2.setText(num2 + "");
                    System.out.println("点击了按钮1");
                } else if (e.getSource() == btn2) {
                    double result = num1 + num2;
                    jTextField3.setText(result + "");
                    System.out.println("点击了按钮2");
                }
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btn1) {
                    jTextField1.setText(num1 + "");
                    jTextField2.setText(num2 + "");
                    System.out.println("点击了按钮1");
                } else if (e.getSource() == btn2) {
                    double result = num1 + num2;
                    jTextField3.setText(result + "");
                    System.out.println("点击了按钮2");
                }
            }
        });

        jPanel2.setLayout(new FlowLayout());
        jPanel2.add(btn1);
        jPanel2.add(btn2);

        jFrame.add(jPanel1, BorderLayout.CENTER);
        jFrame.add(jPanel2, BorderLayout.SOUTH);

        jFrame.setSize(400, 200);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }


}
